package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.entity.Travel;
import com.entity.TravelEs;
import com.repository.TravelRepository;
import com.util.DateUtils;
import com.util.ElasticsearchUtil;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *es从数据库中批量添加
 * </p>
 *
 * @author zhangqing
 * @since 2021/3/17
 */
@RestController
@RequestMapping("/es")
public class ElasticBatchAddController {
    @Autowired
    private TravelRepository travelRepository;
    @Autowired
    private RestTemplate restTemplate;

//    private TravelEsRepository esRepository;
//    private ElasticsearchUtil elasticsearchUtil;
    @GetMapping("/add")
    public void add(){
        List<Travel> travels = travelRepository.selectList(null);
        for (Travel travel : travels) {
            travel.setId("F5F85397F4244D33B85B7BFC44B88244");
         String date =    travel.getDay()+" "+travel.getTime();
            Date parse = DateUtils.parse(date, "yyyy-MM-dd HH:mm:ss");
            long time = parse.getTime();
            travel.setGmtCreate(time);
            travel.setDay(null);
            travel.setTime(null);
        }
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        httpHeaders.set("Authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiIwIiwidXNlcl9kZXRhaWxzIjp7InBhc3N3b3JkIjpudWxsLCJ1c2VybmFtZSI6IjAiLCJhdXRob3JpdGllcyI6W3siYXV0aG9yaXR5IjoiZmNmZjhiZDYxN2ViYzMwZWFjZTM2YzFjYWFjOGEzYTkifV0sImFjY291bnROb25FeHBpcmVkIjp0cnVlLCJhY2NvdW50Tm9uTG9ja2VkIjp0cnVlLCJjcmVkZW50aWFsc05vbkV4cGlyZWQiOnRydWUsImVuYWJsZWQiOnRydWUsInVzZXJJZCI6IjAifSwic2NvcGUiOlsib3BlbmlkIl0sImV4cCI6MTYxNTk4MTUxMywiaWF0IjoxNjE1OTQ1NTEzLCJhdXRob3JpdGllcyI6WyJmY2ZmOGJkNjE3ZWJjMzBlYWNlMzZjMWNhYWM4YTNhOSJdLCJqdGkiOiI0MDc3MTAzMi04Y2FjLTRlMzMtYTJkOC03NmNhZThhY2JiNjMiLCJjbGllbnRfaWQiOiJ3ZWItYXBwIn0.4pBNByGMn_eLewOK6zMwc_Tt1NDn1W2zpReNFHWon4A");
        HttpEntity httpEntity  = new HttpEntity(JSONObject.toJSONString(travels),httpHeaders);
         restTemplate.postForEntity("http://127.0.0.1:6093/v1/gis/addes", httpEntity, String.class);

    }



}
